package com.panshi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panshi.entity.AppAnswer;
import com.panshi.entity.SysAnswerCheck;
import com.panshi.mapper.AnswerCheckMapper;
import com.panshi.service.AnswerCheckService;
import com.panshi.service.AnswerService;
import com.panshi.service.QaFeignClient;
import com.panshi.tools.DataResult;
import com.panshi.tools.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerCheckServiceImpl implements AnswerCheckService {

    @Autowired
    private AnswerCheckMapper answerCheckMapper;

    @Autowired
    private AnswerService answerService;

    @Autowired
    private QaFeignClient qaFeignClient;

    // 上架状态
    private String ANSWER_GROUD = "1";
    // 下架状态
    private String ANSWER_DOWN = "4";

    @Override
    public DataResult addAnswer(SysAnswerCheck sysAnswerCheck) {
        SysAnswerCheck sys = answerCheckMapper.isAdd(sysAnswerCheck.getMatterId(),sysAnswerCheck.getUserId());
        if(null != sys){
            return new DataResult().setCode(sys.getAnswerId()).setMsg("你已经发表过该回答，可在个人中心查看~");
        }
        String str = sysAnswerCheck.getAnswerContent();
        if(str.length() >= 30){
            str = sysAnswerCheck.getAnswerContent().substring(0,30);
        }
        sysAnswerCheck.setAnswerSynopsis(str).setAnswerStatus("2");
        answerCheckMapper.addAnswer(sysAnswerCheck);
        return new DataResult().setCode(sysAnswerCheck.getAnswerId()).setMsg("新增成功");
    }

    @Override
    public DataResult adoptCheck(SysAnswerCheck sysAnswerCheck) {
        answerCheckMapper.adoptCheck(sysAnswerCheck);
        if(ANSWER_GROUD.equals(sysAnswerCheck.getAnswerStatus())){ // 上架
            SysAnswerCheck sys = answerCheckMapper.queryInfo(sysAnswerCheck.getAnswerId());
            System.out.println(sys);
            if(null != sys){
                AppAnswer an = answerService.queryInfo(sysAnswerCheck.getAnswerId());
                if(null == an){
                    // 增加到回答表
                    answerService.addAnswer(sys);
                    // 增加搜索表

                    // 增加动态表

                    // 增加回答数量
                    qaFeignClient.updateAnswerCount(sys.getMatterId(),"answer");
                }

            }
        }else if(ANSWER_DOWN.equals(sysAnswerCheck.getAnswerStatus())){ // 下架
            AppAnswer appAnswer = answerService.queryInfo(sysAnswerCheck.getAnswerId());
            if(null != appAnswer){ // 删除回答表
                answerService.deleteAnswer(sysAnswerCheck.getAnswerId());
            }
        }

        return new DataResult().setCode(sysAnswerCheck.getAnswerId()).setMsg("执行成功");
    }


    @Override
    public DataResult updateAnswer(SysAnswerCheck sysAnswerCheck) {
        sysAnswerCheck.setAnswerSynopsis(sysAnswerCheck.getAnswerContent().substring(0,100));
        SysAnswerCheck sys = answerCheckMapper.queryInfo(sysAnswerCheck.getAnswerId());
        if(null != sys){
            // 执行修改
            answerCheckMapper.updateAnswer(sysAnswerCheck);

            // 修改时不是上架状态。重新进行审核
            if(!ANSWER_GROUD.equals(sysAnswerCheck.getAnswerStatus())){
                return new DataResult().setCode(sysAnswerCheck.getAnswerId()).setMsg("修改成功请等待审核");
            }
            // 同步到回答表
            answerService.addAnswer(sys);
        }
        return new DataResult().setCode(sysAnswerCheck.getAnswerId()).setMsg("修改成功");
    }

    @Override
    public DataResult deleteAnswer(Integer answerId) {
        SysAnswerCheck sys = answerCheckMapper.queryInfo(answerId);
        if(ANSWER_DOWN.equals(sys.getAnswerStatus())){
           return new DataResult().setCode(answerId).setMsg("该数据已删除~");
        }else{
            // 逻辑删除未审核表
            answerCheckMapper.deleteAnswer(answerId);
            // 删除回答表
            AppAnswer appAnswer = answerService.queryInfo(answerId);
            if(null != appAnswer){
                answerService.deleteAnswer(answerId);
            }
            // 减少回答数量
            qaFeignClient.updateAnswerCount(sys.getMatterId(),"answerDelete");
        }
        return new DataResult().setCode(answerId).setMsg("删除成功");
    }

    @Override
    public DataResult queryByUid(Integer userId, PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<SysAnswerCheck> sys = answerCheckMapper.queryByUid(userId);
        for(SysAnswerCheck s: sys){
            AppAnswer answer = answerService.queryInfo(s.getAnswerId());
            if(answer != null && s.getAnswerId().equals(answer.getAnswerId())){
                s.setCommentCount(answer.getCommentCount());
                s.setPraiseCount(answer.getPraiseCount());
                s.setTreadCount(answer.getTreadCount());
            }
            s.setAppMatterCheck(qaFeignClient.queryInfo(s.getMatterId()));
        }
        PageInfo pageInfo = new PageInfo(sys);
        return new DataResult().setCode(200).setData(pageInfo);
    }

    @Override
    public DataResult query(Integer status,PageBean pageBean) {
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<SysAnswerCheck> sys = answerCheckMapper.query(status);
        PageInfo pageInfo = new PageInfo(sys);
        return new DataResult().setCode(0).setCount(1000).setData(pageInfo);
    }

}
